﻿unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dxSkinsCore, dxSkinsdxRibbonPainter, cxClasses, dxRibbon, cxControls,
  dxRibbonForm, cxLookAndFeels, dxSkinsForm, dxStatusBar, dxRibbonStatusBar,
  dxSkinSeven, dxSkinsDefaultPainters, dxSkinsdxBarPainter, dxBar,
  cxLookAndFeelPainters, dxSkinsLookAndFeelPainter, cxGrid,
  cxDropDownEdit, cxBarEditItem, cxGraphics, cxContainer, cxEdit, cxTextEdit,
  cxMaskEdit, dxBarExtDBItems, scin, IniFiles,
  cxLocalization, cxButtonEdit, NewPrincipFrm, JvComponentBase, JvAppStorage,
  JvAppIniStorage, JvFormPlacement, dxSkinMcSkin, cxPropertiesStore,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
   dxLayoutLookAndFeels, cxCheckBox, DistrictFrm, cxBlobEdit, dxSkinBlack,
  dxSkinBlue, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinFoggy, dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinPumpkin, dxSkinSharp,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinValentine, dxSkinXmas2008Blue;

type
  TMainForm = class(Tdxcustomribbonform)
    RibbonTab: TdxRibbonTab;
    Ribbon: TdxRibbon;
    stbar1: TdxRibbonStatusBar;
    SkinCntr1: TdxSkinController;
    barmen1: TdxBarManager;
    btn_scin: TdxBarButton;
    barmen1Bar2: TdxBar;
    cb2: TcxBarEditItem;
    localiz1: TcxLocalizer;
    rep: TdxRibbonTab;
    barmen1Bar3: TdxBar;
    btn1: TdxBarButton;
    bar2: TdxBar;
    btn2: TdxBarButton;
    barmen1Bar1: TdxBar;
    btn3: TdxBarButton;
    grp1: TdxBarGroup;
    btn_Princip: TdxBarButton;
    spr: TdxRibbonTab;
    barmen1Bar4: TdxBar;
    btn_schetfrm: TdxBarButton;
    btn_EventRep: TdxBarButton;
    btn_EventGrRep: TdxBarButton;
    btn_UserForm: TdxBarButton;
    btn_AddrForm: TdxBarButton;
    btn_ETypeForm: TdxBarButton;
    FormStorage1: TJvFormStorage;
    AppIniFileStorage1: TJvAppIniFileStorage;
    str1: TdxBarScreenTipRepository;
    bst_schet: TdxBarScreenTip;
    br_schet: TdxBar;
    br_collChange: TdxBar;
    barPerm: TdxBar;
    btn4: TdxBarButton;
    btn_newrep: TdxBarButton;
    bar_newrep: TdxBar;
    prop1: TcxPropertiesStore;
    btn_AgentRepForm: TdxBarButton;
    AgentBar: TdxBar;
    rtOper: TdxRibbonTab;
    brOper: TdxBar;
    btn_ScanEvFrm: TdxBarButton;
    btn_MoveCollectorForm: TdxBarButton;
    btn5: TdxBarButton;
    LayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    LayoutCxLookAndFeel1: TdxLayoutCxLookAndFeel;
    btn_PhoneForm: TdxBarButton;
    btn_DISTRICT: TdxBarButton;
    dxBarButton1: TdxBarButton;
    btn_PrincipalRepForm: TdxBarButton;
    btn_srv: TdxBarButton;
    btn_CollectorForm: TdxBarButton;
    procedure cb2Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure barmen1ClickItem(Sender: TdxBarManager; ClickedItem: TdxBarItem);
    procedure btn2Click(Sender: TObject);
    procedure btn_PrincipClick(Sender: TObject);
    procedure btn_schetfrmClick(Sender: TObject);
    procedure btn_EventRepClick(Sender: TObject);
    procedure btn_EventGrRepClick(Sender: TObject);
    procedure btn_UserFormClick(Sender: TObject);
    procedure btn_AddrFormClick(Sender: TObject);
    procedure btn_ETypeFormClick(Sender: TObject);
    procedure btn_newrepClick(Sender: TObject);
    procedure btn_AgentRepFormClick(Sender: TObject);
    procedure btn_ScanEvFrmClick(Sender: TObject);
    procedure btn_MoveCollectorFormClick(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn_PhoneFormClick(Sender: TObject);
    procedure btn_DISTRICTClick(Sender: TObject);
    procedure RibbonHelpButtonClick(Sender: TdxCustomRibbon);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dxBarButton1Click(Sender: TObject);
    procedure btn_PrincipalRepFormClick(Sender: TObject);
    procedure btn_srvClick(Sender: TObject);
    procedure btn_CollectorFormClick(Sender: TObject);
  private
    { Private declarations }
    procedure OnHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    {function FindChild(AName :string) :TForm;
    procedure DownButton(AName :string);}
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses NewReestrFrm, CollectorRepFrm, SchetFrm, EventRepFrm,
  EventGrRepFrm, cmUtils, UserFrm, AddrFrm, ETypeFrm, NewReport, AgentRepFrm,
  ScanEventFrm, MoveCollector, NewScanEventFrm, SetPhoneFrm, TT_AnalizFrm,
  PrincipalRepFrm, SrvKontrFrm, CollectorReportForm;


{$R *.dfm}
{==============================================================================
  OnHotKey-> регистрация горячих клавиш
===============================================================================}
procedure TMainForm.OnHotKey(var Msg: TWMHotKey);
var
  HFile :string;
  Id_context :Integer;
begin
   inherited;
    if (Msg.HotKey=1)  then
    begin
      if Application.MainForm.ActiveMDIChild=nil then Exit;
      Id_context:=Application.MainForm.ActiveMDIChild.Tag;
      if Id_context<>0 then
      begin
        HFile:=ExtractFilePath(Application.ExeName)+'Help\manager.chm';
        if FileExists(HFile) then
        HtmlHelp(GetDesktopWindow(),HFile,HH_HELP_CONTEXT,Id_context);
      end;
    end;
end;



procedure TMainForm.barmen1ClickItem(Sender: TdxBarManager;
  ClickedItem: TdxBarItem);
var
  i :Integer;
begin
 if FindChild(ClickedItem.Description)<>nil then
 begin
     for I := 0 to bar2.ItemLinks.AvailableItemCount - 1 do
     begin
       (bar2.ItemLinks.Items[i].Item as TdxBarButton).Lowered:=False;
     end;
   FindChild(ClickedItem.Description).BringToFront;
  (ClickedItem as TdxBarButton).Lowered:=True;
 end;
end;

//отчет по коллекторам
procedure TMainForm.btn1Click(Sender: TObject);
var
  form :TCollectorRepForm;
  i :Integer;
begin
  if FindChild('CollectorRepForm')<>nil then
  begin
     FindChild('CollectorRepForm').BringToFront;
     DownButton('CollectorRepForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TCollectorRepForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//вызываем реестр
procedure TMainForm.btn2Click(Sender: TObject);
var
  form :TNewReestrForm;
  i :Integer;
begin
  if FindChild('NewReestrForm')<>nil then
  begin
     FindChild('NewReestrForm').BringToFront;
     DownButton('NewReestrForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TNewReestrForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      barmen1.Merge(form.bar1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;
procedure TMainForm.btn5Click(Sender: TObject);
var
  form :TNewScanEventForm;
  i :Integer;
begin
  if FindChild('NewScanEventForm')<>nil then
  begin
     FindChild('NewScanEventForm').BringToFront;
     DownButton('NewScanEventForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TNewScanEventForm.Create( Application );
      Form.FormStyle := fsMDIChild;
     //barmen1.Merge(form.bar1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//AddrForm
procedure TMainForm.btn_AddrFormClick(Sender: TObject);
var
  form :TAddrForm;
  i :Integer;
begin
  if FindChild('AddrForm')<>nil then
  begin
     FindChild('AddrForm').BringToFront;
     DownButton('AddrForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TAddrForm.Create( Application );
      Form.FormStyle := fsMDIChild;
     //barmen1.Merge(form.bar1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;
//AgentRepForm
procedure TMainForm.btn_AgentRepFormClick(Sender: TObject);
var
  form :TAgentRepForm;
  i :Integer;
begin
  if FindChild('AgentRepForm')<>nil then
  begin
     FindChild('AgentRepForm').BringToFront;
     DownButton('AgentRepForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TAgentRepForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      barmen1.Merge(form.barmen1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

procedure TMainForm.btn_CollectorFormClick(Sender: TObject);
var
  form :TCollectorForm;
  i :Integer;
begin
  if FindChild('CollectorForm')<>nil then
  begin
     FindChild('CollectorForm').BringToFront;
     DownButton('CollectorForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TCollectorForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//DistrictForm  Справочник районов
procedure TMainForm.btn_DISTRICTClick(Sender: TObject);
var
  form :TDistrictForm;
  i :Integer;
begin
  if FindChild('DistrictForm')<>nil then
  begin
     FindChild('DistrictForm').BringToFront;
     DownButton('DistrictForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TDistrictForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      //barmen1.Merge(form.barmen1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//ETypeForm
procedure TMainForm.btn_ETypeFormClick(Sender: TObject);
var
  form :TETypeForm;
  i :Integer;
begin
  if FindChild('ETypeForm')<>nil then
  begin
     FindChild('ETypeForm').BringToFront;
     DownButton('ETypeForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TETypeForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
     // barmen1.Merge(form.bar1,false);

    except on EOutOfResources do

    end;
  end;
end;
//EventGrRepForm
procedure TMainForm.btn_EventGrRepClick(Sender: TObject);
var
  form :TEventGrRepForm;
  i :Integer;
begin
  if FindChild('EventGrRepForm')<>nil then
  begin
     FindChild('EventGrRepForm').BringToFront;
     DownButton('EventGrRepForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TEventGrRepForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
     // barmen1.Merge(form.bar1,false);

    except on EOutOfResources do

    end;
  end;
end;

//EventRepForm отчет по мероприятиям
procedure TMainForm.btn_EventRepClick(Sender: TObject);
var
  form :TEventRepForm;
  i :Integer;
begin
  if FindChild('EventRepForm')<>nil then
  begin
     FindChild('EventRepForm').BringToFront;
     DownButton('EventRepForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TEventRepForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
     // barmen1.Merge(form.bar1,false);

    except on EOutOfResources do

    end;
  end;
end;
//MoveCollectorForm
procedure TMainForm.btn_MoveCollectorFormClick(Sender: TObject);
var
  form :TMoveCollectorForm;
  i :Integer;
begin
  if FindChild('MoveCollectorForm')<>nil then
  begin
     FindChild('MoveCollectorForm').BringToFront;
     DownButton('MoveCollectorForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TMoveCollectorForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
      //barmen1.Merge(form.barmen1,false);
    except on EOutOfResources do

    end;
  end;
end;

//NewRepForm
procedure TMainForm.btn_newrepClick(Sender: TObject);
var
  form :TNewRepForm;
  i :Integer;
begin
  if FindChild('NewRepForm')<>nil then
  begin
     FindChild('NewRepForm').BringToFront;
     DownButton('NewRepForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TNewRepForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      barmen1.Merge(form.barmen1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//Загрузка телефонов
procedure TMainForm.btn_PhoneFormClick(Sender: TObject);
var
  form :TSetPhoneForm;
  i :Integer;
begin
  if FindChild('SetPhoneForm')<>nil then
  begin
     FindChild('SetPhoneForm').BringToFront;
     DownButton('SetPhoneForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TSetPhoneForm.Create( Application );
      Form.FormStyle := fsMDIChild;
     //barmen1.Merge(form.bar1,false);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//отчет по принципалам
procedure TMainForm.btn_PrincipalRepFormClick(Sender: TObject);
var
  form :TPrincipalRepForm;
  i :Integer;
begin
  if FindChild('PrincipalRepForm')<>nil then
  begin
     FindChild('PrincipalRepForm').BringToFront;
     DownButton('PrincipalRepForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TPrincipalRepForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

procedure TMainForm.btn_PrincipClick(Sender: TObject);
var
  form :TNewPrincipForm;
  i :Integer;
begin
  if FindChild('NewPrincipForm')<>nil then
  begin
     FindChild('NewPrincipForm').BringToFront;
     DownButton('NewPrincipForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TNewPrincipForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
     // barmen1.Merge(form.bar1,false);

    except on EOutOfResources do

    end;
  end;
end;
// ScanEventFrm
procedure TMainForm.btn_ScanEvFrmClick(Sender: TObject);
var
  form :TScanEventForm;
  i :Integer;
begin
  if FindChild('ScanEventForm')<>nil then
  begin
     FindChild('ScanEventForm').BringToFront;
     DownButton('ScanEventForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TScanEventForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
     // barmen1.Merge(form.bar1,true);

    except on EOutOfResources do

    end;
  end;
end;

procedure TMainForm.btn_schetfrmClick(Sender: TObject);
var
  form :TSchetForm;
  i :Integer;
begin
  if FindChild('SchetForm')<>nil then
  begin
     FindChild('SchetForm').BringToFront;
     DownButton('SchetForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TSchetForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      barmen1.Merge(form.bar1,true);
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;
//TSrvKontrForm
procedure TMainForm.btn_srvClick(Sender: TObject);
var
  form :TSrvKontrForm;
  i :Integer;
begin
  if FindChild('SrvKontrForm')<>nil then
  begin
     FindChild('SrvKontrForm').BringToFront;
     DownButton('SrvKontrForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TSrvKontrForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
    except on EOutOfResources do

    end;
  end;
end;

//UserForm
procedure TMainForm.btn_UserFormClick(Sender: TObject);
var
  form :TUserForm;
  i :Integer;
begin
  if FindChild('UserForm')<>nil then
  begin
     FindChild('UserForm').BringToFront;
     DownButton('UserForm');
  end else
  begin
    try
      SetWaitCursor(true);
      form := TUserForm.Create( Application );
      Form.FormStyle := fsMDIChild;
      SetWaitCursor(false);
     // barmen1.Merge(form.bar1,false);

    except on EOutOfResources do

    end;
  end;
end;

procedure TMainForm.cb2Change(Sender: TObject);
var
  i,n :Integer;
 // Ribbon1: TdxRibbon;
begin
 ColorSheme:=VarToStr(cb2.EditValue);
 Ribbon.ColorSchemeName :=VarToStr(cb2.EditValue);
 barmen1.LookAndFeel.SkinName:=VarToStr(cb2.EditValue);
 for I := 0 to application.mainform.MDIChildCount - 1 do
 begin
   SetSheme(application.mainform.MDIChildren[i]);
  { with
   application.mainform.MDIChildren[i] do
   begin
     for n := 0 to ComponentCount - 1 do
     begin
       SetSheme(application.mainform.MDIChildren[i]);
       if (Components[n].ClassType = TcxGrid) then
       (Components[n] as TcxGrid).LookAndFeel.SkinName:=ColorSheme;
       if (Components[n].ClassType =TdxBarManager)  then
       (Components[n] as TdxBarManager).LookAndFeel.SkinName:=ColorSheme;
       if (Components[n].ClassType =TdxRibbon)  then
       (Components[n] as TdxRibbon).ColorSchemeName:=ColorSheme;
     end;
     //(FindComponent('Ribbon') as TdxRibbon).ColorSchemeName:=ColorSheme;
   end;}
 end;
end;

procedure TMainForm.dxBarButton1Click(Sender: TObject);
var
  Form: TTT_AnalizForm;
begin
  Form:=TTT_AnalizForm.Create(Application);
  Form.Show;
end;

procedure TMainForm.RibbonHelpButtonClick(Sender: TdxCustomRibbon);
var
  HFile :string;
  Id_context :Integer;
begin
      if Application.MainForm.ActiveMDIChild=nil then Exit;
      Id_context:=Application.MainForm.ActiveMDIChild.Tag;
      if Id_context<>0 then
      begin
        HFile:=ExtractFilePath(Application.ExeName)+'Help\manager.chm';
        HtmlHelp(GetDesktopWindow(),HFile,HH_HELP_CONTEXT,Id_context);
      end;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnregisterHotKey(Handle, 1);
  lsReestr.Free;
end;

procedure TMainForm.FormCreate(Sender: TObject);
var
  ini :TIniFile;
begin
 RegisterHotKey(Handle, 1, 0, VK_F1); // F1
  //ShowMessage(ParamStr(1));
 if FileExists(ExtractFilePath(Application.ExeName)+'lang\rus.ini') then
 begin
   localiz1.FileName:=ExtractFilePath(Application.ExeName)+'lang\rus.ini';
   localiz1.Active:=True;
   localiz1.Locale:=1049;
 end;
 if FileExists(ExtractFilePath(Application.ExeName)+'Forms.ini') then
 begin
   ini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'Forms.ini');
   ini.DeleteKey('MainForm','RibbonTab_VisibleIndex');
   ini.Free;
 end;
  prop1.StorageName:=ExtractFilePath(Application.ExeName)+'config\main';
  if FileExists(ExtractFilePath(Application.ExeName)+'config\main') then
  prop1.RestoreFrom;
   SetPerm(UserCod,Name);
 // ShowMessage(GetFileVersion(Application.ExeName));
 //загружаем скины в комбобокс
  if (UserCod='2') or (UserCod='4') then
  begin
    btn_newrep.Visible:=ivAlways;
   // btn_ScanEvFrm.Visible:=ivAlways;
    btn_UserForm.Visible:=ivAlways;
    btn5.Visible:=ivAlways;
    rtOper.Visible:=True;
  end else
  begin
    btn_newrep.Visible:=ivNever;
   // btn_ScanEvFrm.Visible:=ivNever;
    btn_UserForm.Visible:=ivNever;
    rtOper.Visible:=False;
  end;
  if (UserCod='16') or (UserCod='75') or (UserCod='75') then
  begin
   rep.Visible:=True;
   rtOper.Visible:=False;
   spr.Visible:=False;
   btn2.Visible:=ivNever;
   btn1.Visible:=ivNever;
   btn_Princip.Visible:=ivNever;
   btn_AgentRepForm.Visible:=ivNever;
   btn_newrep.Visible:=ivNever;
   btn_EventGrRep.Visible:=ivNever;
  end;

  with cb2 do
  dxSkinsUserSkinPopulateSkinNames(ExtractFilePath(Application.ExeName)+
  'scin.skinres',(Properties as TcxComboBoxProperties).Items);
  if cb2.EditValue<>'' then ColorSheme:=cb2.EditValue
  else ColorSheme:=Ribbon.ColorSchemeName;
  // версия - база
  stbar1.Panels[0].Text:=GetFileVersionEx(Application.ExeName);
  stbar1.Panels[1].Text:=DbName;
  {if ParamStr(1)='192.168.5.201:d:\base\rk.fdb' then
   stbar1.Panels[1].Text:='Главная база';
  if ParamStr(1)='192.168.5.200:d:\base\rk.fdb' then
  stbar1.Panels[1].Text:='Тестовая база';
  if ParamStr(1)='192.168.5.200:d:\base\rk1.fdb' then
  stbar1.Panels[1].Text:='База Тюмень'; }
end;

end.
